const { Banner,Category,Goods } = require("../models.js");
const Router = require('koa-router');
let router = new Router({
    prefix:'/index'
})

/* 轮播图 */
router.get('/banner',async(ctx)=>{
    try {
        await Banner.findAll().then(res=>{
            ctx.body = {
                code:200,
                msg:res
            }
        })
    } catch (error) {
        ctx.body = {
            code:400,
            msg:error
        }
    }
})

/* 首页分类数据 */
router.get('/category',async(ctx)=>{
    try {
        await Category.findAll().then(res=>{
            ctx.body = {
                code:200,
                msg:res
            }
        })
    } catch (error) {
        ctx.body = {
            code:400,
            msg:error
        }
    }
})

/* 商品推荐 */
router.get('/recommend',async(ctx)=>{
    try {
        await Goods.findAll({ 
            limit:8
        }).then(res=>{
            ctx.body = {
                code:200,
                msg:res
            }
        })
    } catch (error) {
        ctx.body = {
            code:400,
            msg:error
        }
    }
})


/* 热卖商品 */
router.get('/hot',async(ctx)=>{
    try {
        await Goods.findAll({
            order:[
                ['PID','DESC']
            ],
            limit:10
        }).then(res=>{
            ctx.body = {
                code:200,
                msg:res
            }
        })
    } catch (error) {
        ctx.body = {
            code:400,
            msg:error
        }
    }
})



module.exports = router;